"use server";

import Title from "antd/es/typography/Title";
import { message } from "antd";
import { listQuestionVoByPageUsingPost } from "@/api/questionController";
import QuestionTable from "@/components/QuestionTable/page";

/**
 * 题目列表页面
 */

export default async function QuestionsPage({ searchParams }) {
  // 获取 url 的查询参数
  const { q: searchTitle } = searchParams;
  let questionList = [];
  let total = 0;
  try {
    const res = await listQuestionVoByPageUsingPost({
      title: searchTitle,
      pageSize: 10,
      sortField: "createTime",
      sortOrder: "descend",
    });
    questionList = res.data.records ?? [];
    total = res.data.total ?? 0;
  } catch (e) {
    message.error("获取题目列表失败");
  }
  return (
    <div id="questionPage">
      <Title level={3}>题目列表大全</Title>
      <QuestionTable
        defaultQuestions={questionList}
        defaultTotal={total}
        defaultSearchParams={{ title: searchTitle }}
      />
    </div>
  );
}
